package com.xuzhijie.pms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xuzhijie.pms.common.Result;
import com.xuzhijie.pms.entity.Administrator;
import com.xuzhijie.pms.entity.Role;
import com.xuzhijie.pms.mapper.RoleMapper;
import com.xuzhijie.pms.service.IAdministratorService;
import com.xuzhijie.pms.service.IRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 许志杰
 * @since 2024-02-12
 */
@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {

    @Autowired
    private IRoleService service;

    @Autowired
    private IAdministratorService iAdministratorService;

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer page,
                           @RequestParam Integer size,
                           @RequestParam String name,
                           @RequestParam String authorityString,
                           @RequestParam String status,
                           @RequestParam(required = false) Date startTime,
                           @RequestParam(required = false) Date endTime){

        return service.findPage(page,size,name,authorityString,status,startTime,endTime);
    }

    @DeleteMapping("/delete")
    public Result deleteAdmin(@RequestBody String[] ids){
        return service.deleteAdmin(ids);
    }

    @PutMapping("/edit")
    public Result editRole(@RequestBody Role role){
        return service.editRole(role);
    }

    @PutMapping("/create")
    public Result createRole(@RequestBody Map<String, String> requestBody){
        String roleName = requestBody.get("roleName");
        String authorityString = requestBody.get("authorityString");
        String status = requestBody.get("status");
        String funcMenu = requestBody.get("funcMenu");
        return service.createRole(roleName,authorityString,status,funcMenu);
    }

    @GetMapping("/authority")
    public Result getAuthority(@RequestParam String id,@RequestParam Integer index){
        Administrator byId = iAdministratorService.getById(id);
        Integer authority = byId.getAuthority();
        Role role = service.getById(authority);
        String funcMenu = role.getFuncMenu();
        char[] a = new char[funcMenu.length()];
        for (int i = 0; i < funcMenu.length(); i++) {
            a[i] = funcMenu.charAt(i);
        }
        return Result.success(a[index]);
    }

    @GetMapping("/authority-all")
    public Result getAllAuthority(@RequestParam String id){
        Administrator byId = iAdministratorService.getById(id);
        Integer authority = byId.getAuthority();
        Role role = service.getById(authority);
        String funcMenu = role.getFuncMenu();
        char[] a = new char[funcMenu.length()];
        for (int i = 0; i < funcMenu.length(); i++) {
            a[i] = funcMenu.charAt(i);
        }
        return Result.success(a);
    }

    @GetMapping("/getRole")
    public Result getRole(){
//        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        List<Role> list = service.list();
        List<Role> collect = list.stream().filter(s -> s.getRoleId() != 1).collect(Collectors.toList());

//        Map<Object, Object> map = new HashMap<>();
//        for (int i = 0; i < collect.size(); i++) {
//            map.put(collect.get(i).getRoleId(),collect.get(i).getRoleName());
////            map.put("name",collect.get(i).getRoleName());
//        }
//        System.out.println("map:"+map);
        return Result.success(collect);
    }
}

